package com.example.controller;

import com.example.pojo.Brand;
import com.example.pojo.ResultBean;
import com.example.req.BrandReq;
import com.example.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("list")
    public ResultBean list(BrandReq brandReq){
        System.out.println(brandReq.toString());
        PageHelper.startPage(brandReq.getPageNum(),brandReq.getPageSize());
        List<Brand> brands = brandService.queryByCondition(brandReq);
        PageInfo pageInfo = new PageInfo(brands);
        return ResultBean.success(pageInfo);
    }

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        List<Brand> brands = brandService.queryByCondition(null);
        return ResultBean.success(brands);
    }

    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Brand brand){
        System.out.println("新增或更新的数据：" + brand);
        int rs = brandService.saveOrUpdate(brand);

        return rs == 1 ? ResultBean.success() : ResultBean.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的ID：" + id);
        int rs = brandService.deleteById(id);
        return rs == 1 ? ResultBean.success() : ResultBean.error("服务器异常，删除失败");
    }
}
